// 获取元素
// 添加事件侦听
// 修改DOM
document.addEventListener('DOMContentLoaded', function () {
    const addBtn = document.querySelector(".addtask .addbtn");
    const inputText = document.querySelector(".addtask input");
    const ul = document.querySelector(".tasklist ul");
    const category = document.querySelector(".category");
    const inputSearch = document.querySelector(".search input");

    addBtn.addEventListener('click', function () {
        const taskText = inputText.value.trim();
        if (taskText !== "") {
            TodoList.addTask(taskText);
            inputText.value = '';
        } else {
            alert('请输入添加任务信息！');
        }
    });

    category.addEventListener('click', function (e) {
        if (e.target.tagName === "BUTTON") {
            const filter = e.target.dataset.filter;
            TodoList.renderTasks(TodoList.filterTasks(filter));
        }
    });

    inputSearch.addEventListener('keydown', function (e) {
        if (e.key === "Enter") {
            const query = inputSearch.value.trim();
            if (query !== "") {
                const tasks = TodoList.getTasks();
                const searchedTasks = tasks.filter(task => task.text.includes(query));
                TodoList.renderTasks(searchedTasks);
            } else {
                alert('请在搜索框中输入搜索信息！');
            }
        }
    });

    ul.addEventListener('click', function (e) {
        if (e.target.classList.contains("delBtn")) {
            TodoList.removeTask(e.target.dataset.id);
        }
        if (e.target.classList.contains("editBtn")) {
            Modal.showEditAlert(e.target.dataset.id, e.target.closest('li').querySelector('span').textContent);
        }
        if (e.target.type === "checkbox") {
            TodoList.changeStatus(e.target.dataset.id, e.target.checked);
        }
    });

    // 初始化渲染
    TodoList.renderTasks(TodoList.filterTasks());
});